package com.cnsoft.portal.saas;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.cnsoft.framework.beans.MyBeanFactoryHelper;
import org.cnsoft.framework.beans.common.RESTResultBean;
import org.cnsoft.framework.beans.user.LoginerBean;
import org.cnsoft.framework.beans.user.UserBean;
import org.cnsoft.framework.cache.session.SessionHelper;
import org.cnsoft.framework.support.MyTokenCommonSupport;
import org.cnsoft.framework.token.TokenBusinessSupport;
import org.cnsoft.framework.token.TokenServiceImpl;
import org.cnsoft.framework.utils.DateHelper;
import org.cnsoft.framework.utils.EmptyHelper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.cnsoft.portal.login.ISManagerLoginService;

/**
 * 超管登录
 * 
 * @author ZmSoft
 * @version 0.1.0 2018/3/1
 * @since 0.1.0 2018/3/1
 */
@Controller
// @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@RequestMapping(value = "/pc/1.0/saas")
public class PageSaasLoginController extends MyTokenCommonSupport {

	@RequestMapping(value = "/index.do", method = RequestMethod.POST)
	public ModelAndView doManagerIndexPOST(HttpServletRequest request, HttpServletResponse response, String token, String message, LoginerBean loginer) throws Exception {
		ModelAndView modelAndView = new ModelAndView("/login");

		// 游客Token
		TokenServiceImpl _TokenBusinessSupport_ = TokenServiceImpl.defaultToken(request);
		modelAndView.addObject("token", _TokenBusinessSupport_.getToken());

		modelAndView.addObject("DDD", DateHelper.currentTimeMillisCN1());
		modelAndView.addObject("token", token);
		modelAndView.addObject("loginer", loginer);
		modelAndView.addObject("message", message);

		return modelAndView;
	}

	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	/**
	 * 用户登录页面
	 * 
	 * @param request
	 * @param response
	 * @param loginer
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/login.do", method = RequestMethod.GET)
	public ModelAndView doManagerLoginGET(HttpServletRequest request, HttpServletResponse response, String token, String message, LoginerBean loginer) throws Exception {
		return doManagerIndexPOST(request, response, token, message, loginer);
	}

	/**
	 * 用户登录
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/login.do", method = RequestMethod.POST)
	public ModelAndView doManagerLoginPOST(HttpServletRequest request, HttpServletResponse response, String token, UserBean loginer) throws Exception {
		ModelAndView modelAndView = new ModelAndView("/saas/home");
		// 管理员登录
		ISManagerLoginService myManagerLoginBiz = MyBeanFactoryHelper.getBean(ISManagerLoginService.class);

		// 开始登录过程
		RESTResultBean<UserBean> result = myManagerLoginBiz.doLogin(request, response, loginer);

		if (result.getCode() != 0) {
			// 失败返回首页
			return doManagerLoginGET(request, response, token, result.getMsg(), loginer);
		}
		TokenBusinessSupport tokenBean = (TokenBusinessSupport) result.getData();
		modelAndView.addObject("DDD", DateHelper.currentTimeMillisCN1());
		modelAndView.addObject("MENU", result.getData());
		modelAndView.addObject("token", tokenBean.getToken());
		modelAndView.addObject("loginer", tokenBean.currentUser());

		return modelAndView;
	}

	/**
	 * 用户退出登录
	 * 
	 * @param request
	 * @param response
	 * @param loginer
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/logout.do", method = RequestMethod.GET)
	public ModelAndView doManagerLogoutGET(HttpServletRequest request, HttpServletResponse response, String jobId, String token) throws Exception {
		return doManagerLogoutPOST(request, response, jobId, token);
	}

	/**
	 * 用户退出登录
	 * 
	 * @param request
	 * @param response
	 * @param loginer
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/logout.do", method = RequestMethod.POST)
	public ModelAndView doManagerLogoutPOST(HttpServletRequest request, HttpServletResponse response, String jobId, String token) throws Exception {

		if (EmptyHelper.isNotEmpty(token) && doCheckToken(request, jobId, token)) {
			SessionHelper.sessionDestroyed();
		}

		ModelAndView model = new ModelAndView("redirect:/");
		return model;
	}

}
